<template>
<div>
<el-card shadow="always">
  <div slot="header">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
      <el-breadcrumb-item>内容管理</el-breadcrumb-item>
    </el-breadcrumb>
  </div>
  <!-- 主体是表单 -->
  <el-form label-width="80px">
      <el-form-item label="状态：">
        <el-radio-group v-model="reqParams.status">
          <!-- 文章状态，0-草稿，1-待审核，2-审核通过，3-审核失败，不传为全部 -->
          <el-radio label="null">全部</el-radio>
          <el-radio label="0">草稿</el-radio>
          <el-radio label="1">待审核</el-radio>
          <el-radio label="2">审核通过</el-radio>
          <el-radio label="3">审核失败</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="频道：">
        <GeekChannel v-model="reqParams.channel_id" />
      </el-form-item>
      <el-form-item label="日期：">
        <el-date-picker
        v-model="dateArr"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="filterArticles()">筛选</el-button>
      </el-form-item>
     </el-form>
</el-card>
<!-- 筛选结果 -->
<el-card shadow="always" style="margin-top: 20px;">
  <div slot="header">根据筛选条件共查询到 {{total}} 条结果</div>
  <!-- 表格 -->
   <el-table :data="articleList">
    <!-- 封面 标题 状态 发布时间 阅读数 评论数 点赞数 操作 -->
     <el-table-column label="封面" >
      <template slot-scope="scope">
        <el-image
          style="width: 100px; height: 100px"
          fit="cover"
          :src="scope.row.cover.images[0]"
          :preview-src-list="scope.row.cover.images">
          <div slot="error" class="image-slot">
            <i class="el-icon-picture-outline"></i>
          </div>
        </el-image>
      </template>
     </el-table-column>
     <el-table-column label="标题" prop="title" width="200"></el-table-column>
      <el-table-column label="状态" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" type="info">草稿</el-tag>
          <el-tag v-else-if="scope.row.status === 1" >待审核</el-tag>
          <el-tag v-else-if="scope.row.status === 2" type="success">审核通过</el-tag>
          <el-tag v-else-if="scope.row.status === 4" type="danger">审核失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" prop="pubdate"></el-table-column>
      <el-table-column label="阅读数" prop="read_count"></el-table-column>
      <el-table-column label="评论数" prop="comment_count"></el-table-column>
      <el-table-column label="点赞数" prop="like_count"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="$router.push(`/publish/${scope.row.id}`)" plain type="primary" icon="el-icon-edit" circle></el-button>
          <el-button @click="deleteArticle(scope.row.id)" plain type="danger" icon="el-icon-delete" circle></el-button>
        </template>
      </el-table-column>
   </el-table>
   <!-- 分页 -->
   <el-pagination
      :page-sizes="[2, 3, 5, 10]"
      :page-size.sync="reqParams.per_page"
      :current-page.sync="reqParams.page"
      @current-change="getArticleList()"
      @size-change="getArticleList()"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
</el-card>
</div>
</template>
<script>

export default {
  name: 'ArticleView',
  data () {
    return {
      // 存放数据
      reqParams: {
        status: null,
        channel_id: null,
        begin_pubdate: null,
        end_pubdate: null,
        page: 1, // 当前页码
        per_page: 2 // 页容量
      },
      // channelList: [],
      dateArr: [],
      articleList: [],
      // 文章总数
      total: 0
    }
  },
  created () {
    // 获取频道列表
    // this.getChannelList()
    this.getArticleList()
  },
  methods: {
    // async getChannelList () {
    //   const res = await this.$http.get('/v1_0/channels')
    //   // console.log(res)
    //   this.channelList = res.data.data.channels
    // },
    async getArticleList () {
      const res = await this.$http.get('/v1_0/mp/articles', {
        params: this.reqParams
      })
      // console.log(res)
      this.articleList = res.data.data.results
      this.total = res.data.data.total_count
    },
    // 筛选功能
    filterArticles () {
      this.reqParams.page = 1
      this.getArticleList()
    },
    // 删除文章
    deleteArticle (id) {
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await this.$http.delete(`/v1_0/mp/articles/${id}`)
        this.getArticleList()
        this.$message.success('删除成功')
      }).catch(() => {
        // 用户取消删除后的提示 此处可以不写 但不能没有
      })
    }
  },
  watch: {
    dateArr: {
      deep: true,
      handler (value) {
        // 如果value值为null，表示清空了日期
        if (value === null) {
          // 清空日期
          this.reqParams.begin_pubdate = null
          this.reqParams.end_pubdate = null
        } else {
          // 获取日期数组，分别赋值给请求参数
          // value[0]开始日期 value[1]结束日期
          this.reqParams.begin_pubdate = value[0]
          this.reqParams.end_pubdate = value[1]
        }
      }
    }
  }
}
</script>
<style>
/* 样式 */
</style>
